package com.kinptm.masterclock.adapter.item;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;

import com.kinptm.masterclock.R;
import com.kinptm.masterclock.adapter.ViewBuilder;
import com.kinptm.masterclock.bean.TimerRecord;
import com.kinptm.masterclock.utils.ViewUtil;

import org.joda.time.DateTime;
import org.joda.time.Period;

/**
 * Created by pc on 13-11-13.
 */
public class TimerRecordItemBuilder extends ViewBuilder<TimerRecord> {
    private static final Period END = new Period(DateTime.now(), DateTime.now());

    @Override
    public View createView(LayoutInflater inflater, int position, TimerRecord data) {
        View view = inflater.inflate(R.layout.list_item_timer, null);
        updateView(view, position, data);
        return view;
    }

    @Override
    public void updateView(View view, int position, TimerRecord data) {
        super.updateView(view, position, data);
        DateTime dateTime = data.getTime();
        Period period = new Period(DateTime.now(), dateTime);

        period = period.toStandardSeconds().getSeconds() <= 0 ? END : period;

        String remaining = "未启动";
        if (data.isEnable()) {
            remaining = data.isFinish() ? "已完成" : period.toString(data.getPeriodFormatter());

            ProgressBar progressBar = ViewUtil.findView(view, R.id.timer_item_progress);
            int max = data.getPeriod().toStandardSeconds().getSeconds();
            int progress = max - period.toStandardSeconds().getSeconds();

            progressBar.setMax(max);
            progressBar.setProgress(progress);
        }


        ViewUtil.findTextViewAndSetText(view, R.id.timer_item_remaining_tv, remaining);
        ViewUtil.findTextViewAndSetText(view, R.id.timer_item_time_tv, data.getPeriod().toString(data.getPeriodFormatter()));


    }
}
